<?php
/**
 * Created by xtbb.
 * User: Hoolan
 * Date: 2018-12-4
 * Time: 14:35:11
 */
namespace app\api\controller;

use think\Controller;
use think\Db;

class Message extends Controller
{
    /**
     * 搜索功能
     *
     * 描述：根据首页搜索栏输入的工程编号或工程名称进行检索
     * 接收参数：word
     * 接收方式：get
     * 返回结果：rt
     */
    public function search()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //检查是否填写搜索内容
        if(empty($_GET['word']) || !isset($_GET['word']) || $_GET['word']=='' || $_GET['word']==null){
            $_GET['word']='';
        }
        //接收参数
        $word= $_GET['word'];

        //查询
        $res=Db::table('xt_project')->field('id,title,type,unit,is_completion')->where('title|order','like','%'.$word.'%')->select();
        //判断查询结果，设置返回信息
        if ($res){
            //转换类别 1.电网；2.市政；3.商业；4.新源；5.挂靠
            foreach ($res as &$value) {
                switch ($value['type']){
                    case 1:$value['type']='电网';break;
                    case 2:$value['type']='市政';break;
                    case 3:$value['type']='商业';break;
                    case 4:$value['type']='新源';break;
                    case 5:$value['type']='挂靠';break;
                }
            }
            unset($value);

            //设置状态码
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;
        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        $rt['status']=session('status')?true:false;
        //返回结果集
        echo json_encode($rt);
    }

    /**
     * 追踪项目列表页
     *
     * 描述：根据页码进行数据查询
     * 接收参数：page type
     * 接收方式：get
     * 返回结果：rt
     */
    public function trackList()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //单次查询数量$num
        $num=10;
        //检查是否填写页码
        if(empty($_GET['page']) || !isset($_GET['page']) || $_GET['page']=='' || $_GET['page']==null || $_GET['page']<1){
            $_GET['page']='1';
        }
        //检查是否填写类别
        if(empty($_GET['type']) || !isset($_GET['type']) || $_GET['type']=='' || $_GET['type']==null || $_GET['type']==6){
            $_GET['type']=[1,2,3,4,5];
        }
        //检查是否填写筛选条件
        if(empty($_GET['filtration']) || !isset($_GET['filtration']) || $_GET['filtration']=='' || $_GET['filtration']==null || $_GET['filtration']>2 ){
            $_GET['filtration']='0';
        }
        //接收参数
        $page= $_GET['page'];
        $type= $_GET['type'];
        $filtration=$_GET['filtration'];
        switch ($filtration){
            case 0:$filtrations= array('is_completion','=',1);break;
            case 1:$filtrations=array('create_time','>=',strtotime(date('Y-m-d',time())));break;//今天的
            case 2:$filtrations=array('create_time','>=',strtotime(date('Y-m-d',strtotime("-1 week"))));break;//近一个星期的
        }
        //设置limit的初值
        $offset=($page-1)*$num;
        //查询条件
        $where=[
            //项目状态码，1代表追踪
            array('is_completion','=',1),
            array('type','in',$type),
            array('delete_time','=',0),
            $filtrations,
        ];
        //查询
        $res=Db::table('xt_project')
//            ->fetchSql()
            ->field('id,title,manager,bid_money')//查询项目id、项目名称、总经理、投标金额
            ->where($where)
            ->limit($offset,$num)
            ->select();
//        dump($res);
//        die();
        //判断查询结果，设置返回信息
        if ($res){
            //处理金额
            foreach ($res as &$value) {
                $value['bid_money']=sprintf("%.2f",$value['bid_money']/10000);
            }
            unset($value);
            //设置状态码
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;
        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        $rt['status']=session('status')?true:false;
        //返回结果集
        echo json_encode($rt);

    }

    /**
     * 追踪项目详情页
     *
     * 描述：根据id查询详情
     * 接收参数：id
     * 接收方式：get
     * 返回结果：rt
     */
    public function trackDetails()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //检查是否填写id
        if(empty($_GET['id']) || !isset($_GET['id']) || $_GET['id']=='' || $_GET['id']==null){
            //设置状态码
            $rt['code']='-10001';
            //设置返回信息
            $rt['message']='初始化失败，请传输正确的参数！';
            //返回结果集，终止程序
            echo json_encode($rt);
            die();
        }
        //接收id
        $id=$_GET['id'];
        //查询条件
        $where=[
            //根据id精确查询
            array('id','=',$id),
            //项目状态码，1代表追踪
            array('is_completion','=','1'),
            array('delete_time','=',0),
        ];
        //查询
        $res=Db::table('xt_project')
            //项目名称、地址、业主名称、投标经办人、基建负责人、投标负责人、总经理、联系方式、投标时间、投标金额、投标内容简述
            ->field('title,address,unit,operator,infrastructure,principal,manager,mobile,bid_time,bid_money,content')
            ->where($where)
            ->find();
        //判断查询结果，设置返回信息
        if ($res){
            //处理金额
            $res['bid_money']=sprintf("%.2f", $res['bid_money']);
            //处理下时间
            $res['bid_time']=date('Y/m/d',$res['bid_time']);
            //设置状态码
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;
        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        //返回结果集
        echo json_encode($rt);
    }

    /**
     * 在建项目列表页
     *
     * 描述：根据页码进行数据查询
     * 接收参数：page type filtration
     * 接收方式：get
     * 返回结果：rt
     */
    public function buildingList()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //单次查询数量$num
        $num=10;
        //检查是否填写页码
        if(empty($_GET['page']) || !isset($_GET['page']) || $_GET['page']=='' || $_GET['page']==null){
            $_GET['page']='1';
        }
        //检查是否填写类别
        if(empty($_GET['type']) || !isset($_GET['type']) || $_GET['type']=='' || $_GET['type']==null || $_GET['type']==6){
            $_GET['type']='1,2,3,4,5';
        }
        //检查是否填写筛选条件
        if(empty($_GET['filtration']) || !isset($_GET['filtration']) || $_GET['filtration']=='' || $_GET['filtration']==null || $_GET['filtration']>5 ){
            $_GET['filtration']='0';
        }
        //接收参数
        $page= $_GET['page'];
        $type= $_GET['type'];
        $filtration=$_GET['filtration'];
        //设置limit的初值
        $offset=($page-1)*$num;
        //设置筛选条件
        switch ($filtration){
            case 0:$filtrations= array('is_completion','=',2);break;
            case 1:$filtrations=array(
                array('contract_amount','>',0),
                array('end_time','<',time()-2592000),
            );break;//应收账款超一个月
            case 2:$filtrations=array('contract_amount','>',1000000);break;//应收账款大于一百万的
            case 3:$filtrations=array('capital','>',1000000);break;//占用资金超一百万的
            case 4:$filtrations=array('create_time','>=',strtotime(date('Y-m-d',time())));break;//今天的
            case 5:$filtrations=array('create_time','>=',strtotime(date('Y-m-d',strtotime("-1 week"))));break;//近一个星期的
        }
        //查询条件
        $where=[
            //项目状态码，2代表在建
            array('is_completion','=',2),
            array('type','in',$type),
            array('delete_time','=','0'),
            $filtrations,

        ];
        //查询
        $res=Db::table('xt_project')
//            ->fetchSql()
            //工程id，工程名称，项目负责人，工程编号，合同金额，未到账金额
            ->field('id,title,leader,order,contract_amount,uncollected')
            ->where($where)
            ->limit($offset,$num)
            ->select();

//        dump($res);
//        die();

        //判断查询结果，设置返回信息
        if ($res){
            //处理金额
            foreach ($res as &$value) {
                $value['contract_amount']=sprintf("%.2f",$value['contract_amount']/10000);
                $value['uncollected']=sprintf("%.2f",$value['uncollected']/10000);
            }
            unset($value);
            //设置状态码
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;
        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        $rt['status']=session('status')?true:false;
        //返回结果集
        echo json_encode($rt);

    }

    /**
     * 在建项目汇总页
     *
     *
     */
    public function summarizingBuildingList()
    {
       $res = Db::table('xt_project')
              ->field('type,uncollected,contract_amount,settlement,capital')
              ->where('delete_time','=',0)
              ->where('is_completion','=',2)
              ->select();
       if ($res){
           $yinshouzhangkuangeshu=0;
           $zanyongzijingeshu=0;
           $zhongjiner1=0;
           $zhongzhangkuan1=0;
           $zhongjiesuanjiner1=0;
           $zhongjiesuanjiner2=0;
           $zhongjiesuanjiner3=0;
           $zhongjiesuanjiner4=0;
           $zhongjiesuanjiner5=0;
           $zhongzanyongzijin1=0;
           $zhongzanyongzijin2=0;
           $zhongzanyongzijin3=0;
           $zhongzanyongzijin4=0;
           $zhongzanyongzijin5=0;
           $zhongjiner2=0;
           $zhongzhangkuan2=0;
           $zhongjiner3=0;
           $zhongzhangkuan3=0;
           $zhongjiner4=0;
           $zhongzhangkuan4=0;
           $zhongjiner5=0;
           $zhongzhangkuan5=0;
           $zongshu1=0;
           $zongshu2=0;
           $zongshu3=0;
           $zongshu4=0;
           $zongshu5=0;
           foreach ($res as &$value) {
                if ($value['uncollected']){
                    $yinshouzhangkuangeshu++;
                }
                if ($value['capital']){
                    $zanyongzijingeshu++;
                }
               if ($value['type']==1){
                   $zhongjiner1 += $value['contract_amount'];
                   $zhongzhangkuan1+= $value['uncollected'];
                   $zhongjiesuanjiner1+=$value['settlement'];
                   $zhongzanyongzijin1+=$value['capital'];
                   $zongshu1+=1;
               }
               if ($value['type']==2){
                   $zhongjiner2 += $value['contract_amount'];
                   $zhongzhangkuan2+= $value['uncollected'];
                   $zhongjiesuanjiner2+=$value['settlement'];
                   $zhongzanyongzijin2+=$value['capital'];
                   $zongshu2+=1;
               }
               if ($value['type']==3){
                   $zhongjiner3 += $value['contract_amount'];
                   $zhongzhangkuan3+= $value['uncollected'];
                   $zhongjiesuanjiner3+=$value['settlement'];
                   $zhongzanyongzijin3+=$value['capital'];
                   $zongshu3+=1;
               }
               if ($value['type']==4){
                   $zhongjiner4 += $value['contract_amount'];
                   $zhongzhangkuan4+= $value['uncollected'];
                   $zhongjiesuanjiner4+=$value['settlement'];
                   $zhongzanyongzijin4+=$value['capital'];
                   $zongshu4+=1;
               }
               if ($value['type']==5){
                   $zhongjiner5 += $value['contract_amount'];
                   $zhongzhangkuan5+= $value['uncollected'];
                   $zhongjiesuanjiner5+=$value['settlement'];
                   $zhongzanyongzijin5+=$value['capital'];
                   $zongshu5+=1;
               }
           }
           unset($value);
            $rt=array(

                    array(
                        'contract_amount'=>sprintf("%.2f",($zhongjiner1+$zhongjiner2+$zhongjiner3+$zhongjiner4+$zhongjiner5)/10000),
                        'uncollected'=>sprintf("%.2f",($zhongzhangkuan1+$zhongzhangkuan2+$zhongzhangkuan3+$zhongzhangkuan4+$zhongzhangkuan5)/10000),
                        'settlement'=>sprintf('%.2f',($zhongjiesuanjiner1+$zhongjiesuanjiner2+$zhongjiesuanjiner3+$zhongjiesuanjiner4+$zhongjiesuanjiner5)/10000),
                        'capital'=>sprintf('%.2f',($zhongzanyongzijin1+$zhongzanyongzijin2+$zhongzanyongzijin3+$zhongzanyongzijin4+$zhongzanyongzijin5)/10000)
                    ),
                    array(
                        'contract_amount'=>sprintf("%.2f",($zhongjiner1)/10000),
                        'uncollected'=>sprintf("%.2f",($zhongzhangkuan1)/10000),
                        'settlement'=>sprintf('%.2f',($zhongjiesuanjiner1)/10000),
                        'capital'=>sprintf('%.2f',($zhongzanyongzijin1)/10000),
                        'sum'=>$zongshu1
                    ),
                    array(
                        'contract_amount'=>sprintf("%.2f",($zhongjiner2)/10000),
                        'uncollected'=>sprintf("%.2f",($zhongzhangkuan2)/10000),
                        'settlement'=>sprintf('%.2f',($zhongjiesuanjiner2)/10000),
                        'capital'=>sprintf('%.2f',($zhongzanyongzijin2)/10000),
                        'sum'=>$zongshu2
                    ),
                    array(
                        'contract_amount'=>sprintf("%.2f",($zhongjiner3)/10000),
                        'uncollected'=>sprintf("%.2f",($zhongzhangkuan3)/10000),
                        'settlement'=>sprintf('%.2f',($zhongjiesuanjiner3)/10000),
                        'capital'=>sprintf('%.2f',($zhongzanyongzijin3)/10000),
                        'sum'=>$zongshu3,
                    ),
                    array(
                        'contract_amount'=>sprintf("%.2f",($zhongjiner4)/10000),
                        'uncollected'=>sprintf("%.2f",($zhongzhangkuan4)/10000),
                        'settlement'=>sprintf('%.2f',($zhongjiesuanjiner4)/10000),
                        'capital'=>sprintf('%.2f',($zhongzanyongzijin4)/10000),
                        'sum'=>$zongshu4,
                    ),
                    array(
                        'contract_amount'=>sprintf("%.2f",($zhongjiner5)/10000),
                        'uncollected'=>sprintf("%.2f",($zhongzhangkuan5)/10000),
                        'settlement'=>sprintf('%.2f',($zhongjiesuanjiner5)/10000),
                        'capital'=>sprintf('%.2f',($zhongzanyongzijin5)/10000),
                        'sum'=>$zongshu5,
                    ),
                    'number_uncollected'=>$yinshouzhangkuangeshu,
                    'number_capital'=>$zanyongzijingeshu,
           );

           $rt['status']=session('status')?true:false;
            echo json_encode($rt);
       }
    }

    /**
     * 在建项目详情页
     *
     * 描述：根据id查询详情
     * 接收参数：id
     * 接收方式：get
     * 返回结果：rt
     */
    public function buildingDetails()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //检查是否填写id
        if(empty($_GET['id']) || !isset($_GET['id']) || $_GET['id']=='' || $_GET['id']==null){
            //设置状态码
            $rt['code']='-10001';
            //设置返回信息
            $rt['message']='初始化失败，请传输正确的参数！';
            //返回结果集，终止程序
            echo json_encode($rt);
            die();
        }
        //接收id
        $id=$_GET['id'];
        //11111
        //查询条件
        $where=[
            //根据id精确查询
            array('id','=',$id),
            //项目状态码，2代表在建
            array('is_completion','=',2),
            array('delete_time','=',0),
        ];
        //查询
        $res=Db::table('xt_project')
            //工程编号、工程类别、工程名称、建设单位、项目负责人、合同签订日期、未到款金额、结算金额、合同金额、占用资金、合同到款时间1-5、、合同信息1-5、合同状态1-5
            ->field('order,type,title,unit,leader,people,contract_time,uncollected,settlement,contract_amount,capital,to1_time,to2_time,to3_time,to4_time,to5_time,to1_msg,to2_msg,to3_msg,to4_msg,to5_msg,to1_status,to2_status,to3_status,to4_status,to5_status')
            ->where($where)
            ->find();
        //判断查询结果，设置返回信息
        if ($res){
            //处理下金额
            $res['uncollected']=sprintf("%.2f",$res['uncollected']);
            $res['settlement']=sprintf("%.2f",$res['settlement']);
            $res['contract_amount']=sprintf("%.2f",$res['contract_amount']);
            $res['capital']=sprintf("%.2f",$res['capital']);
            //转换类别 1.电网；2.市政；3.商业；4.新源；5.挂靠
            switch ($res['type']){
                case 1:$res['type']='电网';break;
                case 2:$res['type']='市政';break;
                case 3:$res['type']='商业';break;
                case 4:$res['type']='新源';break;
                case 5:$res['type']='挂靠';break;
            }
            //处理下时间
            $res['contract_time']=date('Y/m/d',$res['contract_time']);
//            for ($i=1;$i<6;$i++)
//            {
//                if ($res['to'.$i.'_time']){
//                    $res['to'.$i.'_time']=date('Y/m/d',$res['to'.$i.'_time']);
//                }
//            }
            //设置状态码
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;
        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        $rt['status']=session('status')?true:false;
        //返回结果集
        echo json_encode($rt);
    }

    /**
     * 应收账款列表页
     *
     * 描述：根据类别进行数据查询
     * 接收参数：type page
     * 接收方式：get
     * 返回结果：rt
     */
    public function billList()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //每页记录数
        $num=10;
        //检查页码
        if(empty($_GET['page']) || !isset($_GET['page']) || $_GET['page']=='' || $_GET['page']==null ||$_GET['page'] < 1){
            $_GET['page']=1;
        }
        //检查是否填写类别
        if(empty($_GET['type']) || !isset($_GET['type']) || $_GET['type']=='' || $_GET['type']==null ||$_GET['type'] == 6){
            $_GET['type']='1,2,3,4,5';
        }
        if(empty($_GET['filtration']) || !isset($_GET['filtration']) || $_GET['filtration']=='' || $_GET['filtration']==null || $_GET['filtration']>4 ){
            $_GET['filtration']='0';
        }
        //接收参数
        $page= $_GET['page'];
        $type= $_GET['type'];
        $filtration=$_GET['filtration'];
        //设置limit的初值
        $offset=($page-1)*$num;
        //设置筛选条件
        switch ($filtration){
            case 0:$filtrations= array('is_completion','=',2);break;
            case 1:$filtrations=array(
                array('contract_amount','>',0),
                array('end_time','<',time()-2592000),
            );break;//应收账款超一个月
            case 2:$filtrations=array('contract_amount','>',1000000);break;//应收账款大于一百万的
            case 3:$filtrations=array('create_time','>=',strtotime(date('Y-m-d',time())));break;//今天的
            case 4:$filtrations=array('create_time','>=',strtotime(date('Y-m-d',strtotime("-1 week"))));break;//近一个星期的
        }
        //查询条件
        $where=[
            //项目状态码，1代表在建
            array('is_completion','=',2),
            array('type','in',$type),
            array('delete_time','=',0),
            $filtrations
        ];
        //查询
        $res=Db::table('xt_project')
            //工程id，工程名称，应收账款，合同到期时间
            ->field('id,title,leader,order,uncollected,create_time,to1_time,to2_time,to3_time,to4_time,to5_time')
            ->where($where)
            ->limit($offset,$num)
            ->select();
        //判断查询结果，设置返回信息
        if ($res){
            foreach ($res as &$value) {
                //处理金额
                $value['uncollected']=sprintf("%.2f",$value['uncollected']/10000);
                //处理下时间
                if ($value['to1_time']){
                    $value['time'] =date('Y/m/d',$value['to1_time']);
                }
                if ($value['to2_time']){
                    $value['time'] =date('Y/m/d',$value['to2_time']);
                }
                if ($value['to3_time']){
                    $value['time'] =date('Y/m/d',$value['to3_time']);
                }
                if ($value['to4_time']){
                    $value['time'] =date('Y/m/d',$value['to4_time']);
                }
                if ($value['to5_time']){
                    $value['time'] =date('Y/m/d',$value['to5_time']);
                }

            }
            unset($value);


            //设置状态码
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;
        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        $rt['status']=session('status')?true:false;
        //返回结果集
        echo json_encode($rt);
    }

    /**
     * 占用列表页
     *
     * 描述：根据页码进行数据查询
     * 接收参数：page type
     * 接收方式：get
     * 返回结果：rt
     */
    public function capitalList()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //单次查询数量$num
        $num=10;
        //检查是否填写页码
        if(empty($_GET['page']) || !isset($_GET['page']) || $_GET['page']=='' || $_GET['page']==null){
            $_GET['page']='1';
        }
        //检查是否填写类别
        if(empty($_GET['type']) || !isset($_GET['type']) || $_GET['type']=='' || $_GET['type']==null || $_GET['type']==6){
            $_GET['type']='1,2,3,4,5';
        }
        if(empty($_GET['filtration']) || !isset($_GET['filtration']) || $_GET['filtration']=='' || $_GET['filtration']==null || $_GET['filtration']>4 ){
            $_GET['filtration']='0';
        }
        //接收参数
        $page= $_GET['page'];
        $type= $_GET['type'];
        $filtration=$_GET['filtration'];
        //设置limit的初值
        $offset=($page-1)*$num;
        //设置筛选条件
        switch ($filtration){
            case 0:$filtrations= array('is_completion','=',2);break;
            case 1:$filtrations=array('contract_amount','>',1000000);break;//占用资金大于一百万的
            case 2:$filtrations=array('create_time','>=',strtotime(date('Y-m-d',time())));break;//今天的
            case 3:$filtrations=array('create_time','>=',strtotime(date('Y-m-d',strtotime("-1 week"))));break;//近一个星期的
        }
        //查询条件
        $where=[
            //项目状态码，2代表在建
            array('is_completion','=',2),
            array('type','in',$type),
            array('delete_time','=',0),
            $filtrations,
        ];
        //查询
        $res=Db::table('xt_project')
            //工程id，工程名称，占用资金
            ->field('id,title,capital')
            ->where($where)
            ->limit($offset,$num)
            ->select();

        //判断查询结果，设置返回信息
        if ($res){
            //处理金额
            foreach ($res as &$value) {
                $value['capital']=sprintf("%.2f",$value['capital']/10000);
            }
            unset($value);
            //设置状态码
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;

        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        $rt['status']=session('status')?true:false;
        //返回结果集
        echo json_encode($rt);




    }

    /**
     * 汇总页
     *
     * 描述：根据年份和完成情况进行数据汇总
     * 接收参数：year completion
     * 接收方式：get
     * 返回结果：rt
     */
    public function summarizing()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //检查是否填写搜索内容
        if(empty($_GET['year']) || !isset($_GET['year']) || $_GET['year']=='' || $_GET['year']==null ||
            empty($_GET['completion']) || !isset($_GET['completion']) || $_GET['completion']=='' || $_GET['completion']==null){
            //设置状态码
            $rt['code']='-10001';
            //设置返回信息
            $rt['message']='初始化失败，请传输正确的参数！';
            //返回结果集，终止程序
            echo json_encode($rt);
            die();
        }
        //接收参数
        $year = $_GET['year'];
        $completion = $_GET['completion'];
        //处理下时间
        $afterYaer=$year+1;//下一年 接收到的时间是当年的，但截止的最后一天应该是下一年第一天和当年的第一天
        $time1 = $afterYaer.'-01-01 00:00:00';//拼接下一年完整截止时间
        $time2 = $year.'-01-01 00:00:00';//拼接当年完整时间
        $afterYaer = strtotime($time1);//字符串转时间戳，与数据库时间进行对比
        $year=strtotime($time2);//字符串转时间戳，与数据库时间进行对比
        //查询条件
        $where=array(
            array('is_completion','=',$completion),
            array('contract_time','<',$afterYaer),
            array('contract_time','>',$year),
            array('delete_time','=',0),
        );
        //查询
        $res=Db::table('xt_project')
//           ->fetchSql()
            //项目id，项目名称，
            ->field('id,title,capital,uncollected,contract_amount,type')
            ->where($where)
            ->select();

        //判断查询结果，设置返回信息
        if ($res){
            //项目数
            $statistics['count']=count($res);
            //总占用资金
            $statistics['capital']=0;
            //总未到款金额
            $statistics['uncollected']=0;
            //总合同总金额
            $statistics['contract_amount']=0;

            foreach ($res as &$value) {
                $statistics['capital']+=$value['capital'];
                $statistics['uncollected']+=$value['uncollected'];
                $statistics['contract_amount']+=$value['contract_amount'];
            }
            unset($value);
            $statistics['contract_amount']=sprintf("%.2f",$statistics['contract_amount']/10000);
            $statistics['uncollected']=sprintf("%.2f",$statistics['uncollected']/10000);
            $statistics['capital']=sprintf("%.2f",$statistics['capital']/10000);
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;
            //设置统计信息
            $rt['statistics']=$statistics;
        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        $rt['status']=session('status')?true:false;
        //返回结果集
        echo json_encode($rt);
    }


    /**
     * 奖金池
     *
     * 描述：计算奖金
     * 接收参数：type page
     * 接收方式：get
     * 返回结果：rt
     */
    public function bonusList(){
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //单次查询数量$num
        $num=10;

        //检查是否填写页码
        if(empty($_GET['page']) || !isset($_GET['page']) || $_GET['page']=='' || $_GET['page']==null){
            $_GET['page']='1';
        }
        //接收参数
        $page=$_GET['page'];
        //设置limit的初值
        $offset=($page-1)*$num;
        //查询条件
        $where=[
            //项目状态码，1代表在建
            array('is_completion','in',array(2,3)),
            array('delete_time','=',0),
        ];
        //查询
        $res=Db::table('xt_project')
            //工程id，工程编号，工程名称，利润，奖金，
            ->field('id,order,title,profit,bonus')
            ->where($where)
            ->limit($offset,$num)
            ->select();

        //判断查询结果，设置返回信息
        if ($res){
            //处理金额
            foreach ($res as &$value) {
               $value['profit']=sprintf("%.2f",$value['profit']);
               $value['bonus']=sprintf("%.2f",$value['bonus']);
            }
            unset($value);
            //设置状态码
            $rt['code']=1;
            //设置返回信息
            $rt['message']=$res;
        }else{
            //设置状态码
            $rt['code']=-10404;
            //设置返回信息
            $rt['message']='未查询到数据！';
        }
        $rt['status']=session('status')?true:false;
        //返回结果集
        echo json_encode($rt);
    }

    public function bonus()
    {
        //结果集rt
        $rt=array(
            'code'=>'-10500',
            'message'=>'服务器运行时发生了错误！'
        );
        //检查是否填写id
        if(empty($_GET['id']) || !isset($_GET['id']) || $_GET['id']=='' || $_GET['id']==null){
            //结果集rt
            $rt=array(
                'code'=>'-10001',
                'message'=>'请填写正确的参数'
            );
            echo json_encode($rt);
            die();
        }
        //查询
        $res=Db::table('xt_project')
            //工程id，工程编号，工程名称，收入，物料支出，劳务支出，税，风险支出，占用资金，发展支出，利润，奖金占比，奖金，
            ->field('order,title,income,material,personnel,tax,risk,develop,capital,profit,percent,bonus')->find();
        if ($res){
            if ($res){
                $res['bonus']=sprintf("%.2f",$res['bonus']);
                $res['income']=sprintf("%.2f",$res['income']);
                $res['material']=sprintf("%.2f",$res['material']);
                $res['personnel']=sprintf("%.2f",$res['personnel']);
                $res['tax']=sprintf("%.2f",$res['tax']);
                $res['develop']=sprintf("%.2f",$res['develop']);
                $res['capital']=sprintf("%.2f",$res['capital']);
                $res['profit']=sprintf("%.2f",$res['profit']);
                $res['risk']=sprintf("%.2f",$res['risk']);
                $rt=array(
                    'code'=>'1',
                    'message'=>$res
                );
            }else{
                $rt=array(
                    'code'=>'10404',
                    'message'=>'未找到数据！'
                );
            }
            $rt['status']=session('status')?true:false;
            echo json_encode($rt);
        }
    }
}
